var util = require('./we7/resource/utils/util.js');
var QQMapWX = require('/we7/resource/utils/qqmap-wx-jssdk.js');
var qqmapsdk;
const app = getApp();

App({
  globalData:{
    StatusBar:123123,
    sum:0,
    location:{},
    url: 'https://lt.sitedown.top/attachment/',
    getPosution:'',
    tabbar: {
      color: "#242424",
      selectedColor: "#F66708",
      backgroundColor: "#ffffff",
      borderStyle: "#d7d7d7",
      list: [
        {
          index: "0",
          pagePath: "/we7/pages/index/index",
          text: "首页",
          iconPath: "https://lt.sitedown.top/attachment/images/images/shouYe.png",
          selectedIconPath: "https://lt.sitedown.top/attachment/images/images//shouYe(1).png",
          selected: true
        },
        {
          index: "1",
          pagePath: "/we7/pages/fenLei/fenLei",
          text: "分类",
          iconPath: "https://lt.sitedown.top/attachment/images/images/fenLei.png",
          selectedIconPath: "https://lt.sitedown.top/attachment/images/images//fenLei(1).png",
          selected: false
        },
        {
          index: "2",
          dott: 5,
          pagePath: "/we7/pages/shoping/shoping",
          text: "购物车",
          iconPath: "https://lt.sitedown.top/attachment/images/images/shoping.png",
          selectedIconPath: "https://lt.sitedown.top/attachment/images/images//shoping(1).png",
          selected: false
        },
        {
          index: "3",
          pagePath: "/we7/pages/mine/mine",
          text: "我的",
          iconPath: "https://lt.sitedown.top/attachment/images/images/mine.png",
          selectedIconPath: "https://lt.sitedown.top/attachment/images/images//mine(1).png",
          selected: false
        }
      ],
      position: "bottom"
    },
    moRenShop:""
  },
  onLaunch: function (res){
    var userInfo = wx.getStorageSync("openid");
    var nickName = wx.getStorageSync("nickName");
    var avatarUrl = wx.getStorageSync("avatarUrl");
    console.log(userInfo)
    if (!userInfo || !nickName || !avatarUrl) {
      var query="";
      var obj = res.query;
      for(var k in obj){
        query+=k+"="+obj[k]+"&"
      }
      console.log('/we7/pages/obtain/obtain?p=' + res.path + "?" + query)
      setTimeout(
        function(){wx.redirectTo({
          url: '/we7/pages/obtain/obtain?p='+res.path+"&"+query
        })},
        2000
      )
    }
  },
  startpay: function (data) {
    wx.requestPayment({
      'timeStamp': data.timeStamp,
      'nonceStr': data.nonceStr,
      'package': data.package,
      'signType': 'MD5',
      'paySign': data.paySign,
      'success': function (res) {
        console.log(data)
        //执行支付成功提示
        console.log(res)
        var that = this
        var userInfo = wx.getStorageSync("openid");
        util.request({
          url: 'admin/WxGoodsApi/send',
          data: {
            number: data.out_trade_no,
            url:'we7/pages/mine/minePSIntegralExchange',
            open_id: userInfo
          },
          method: 'post',
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          success: function (res) {
            console.log(res)
            if (res.data.code == 0) {
              wx.showToast({
                title: res.data.msg,
                icon: 'none',
                duration: 2000
              })
              wx.redirectTo({
                url: '/we7/pages/mine/myOrder?ordertype=2',
              })
            }
          },
          fail: function (res) {
            wx.showToast({
              title: '服务器响应失败',
              icon: 'none',
              duration: 2000
            })
          }
        });
      },
      'fail': function (res) {
        var out_trade_no = data.out_trade_no;
         var that = this

        util.request({
          url: 'admin/WxGoodsApi/serialUmber',
          data: {
            serial_umber: out_trade_no
          },
          method: 'post',
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          success: function (res) {
            console.log(res)
            if (res.data.code == 0) {
              wx.showToast({
                title: res.data.msg,
                icon: 'none',
                duration: 2000
              })
              wx.redirectTo({
                url: '/we7/pages/mine/myOrder?ordertype=1',
              })
            }
          },
          fail: function (res) {
            wx.showToast({
              title: '服务器响应失败',
              icon: 'none',
              duration: 2000
            })
          }
    });
    }
    })
  },
  onShow: function (res){
    console.log(res)
    var userInfo = wx.getStorageSync("openid");
    var nickName = wx.getStorageSync("nickName");
    var avatarUrl = wx.getStorageSync("avatarUrl");
    console.log(userInfo)
    if (!userInfo || !nickName || !avatarUrl) {
      var query = "";
      var obj = res.query;
      for (var k in obj) {
        query += k+"=" + obj[k] + "&"
      }
      console.log(query)
      setTimeout(
        function () {
          wx.redirectTo({
            url: '/we7/pages/obtain/obtain?p=' + res.path + "&" + query
          })
        },
        2000
      )
    }
  },
  setData:function(res){
    this.changeTabBar();
  },
  onHide: function (){
  },
  onError: function (msg) {
    console.log(msg)
  },
  util: util,
  userInfo: {
    sessionid: null,
  },
  changeTabBar:function (){
    var _curPageArr = getCurrentPages();
    var _curPage = _curPageArr[_curPageArr.length - 1];
    var _pagePath = _curPage.__route__;
    if (_pagePath.indexOf('/') != 0) {
      _pagePath = '/' + _pagePath;
    }
    var tabBar = this.globalData.tabbar;
    for (var i = 0; i < tabBar.list.length; i++) {
      tabBar.list[i].selected = false;
      if (tabBar.list[i].pagePath == _pagePath) {
        tabBar.list[i].selected = true;//根据页面地址设置当前页面状态  
      }
    }
    _curPage.setData({
      tabbar: tabBar
    });
  },
  siteInfo: require('siteinfo.js')
});